package com.qf.service.impl;

import com.qf.dao.BusinessMapper;
import com.qf.dao.BusinessRepository;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.*;
import com.qf.service.BusinessService;
import com.qf.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    BusinessRepository businessRepository;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public BaseResp findAll(Address address) {
        BaseResp baseResp = new BaseResp();
        List<Business> all = businessRepository.findAll();
        List<Business> blist3 = ListCaculate.distanceB(all,address);
        baseResp.setData(blist3);
        baseResp.setCode(200);
        return baseResp;
    }
    @Override
    public BaseResp findByType(Integer tid,Address address) {
        BaseResp baseResp = new BaseResp();
        List<BusinessAndType> byType = businessMapper.findByType(tid);
        if(byType!=null){
            List<BusinessAndType> businessAndTypes = ListCaculate.distanceBT(byType, address);
            baseResp.setData(businessAndTypes);
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(2001);
        return baseResp;
    }
    @Override
    public BaseResp findOne(Integer id) {
        BaseResp baseResp = new BaseResp();
        Optional<Business> byId = businessRepository.findById(id);
        if(byId.isPresent()){
            baseResp.setData(byId.get());
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(2001);
        return baseResp;
    }

    @Override
    public BaseResp getEvaluation(Business business) {
        List values = redisTemplate.opsForHash().values(business.getBname());
        //声明返回值
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setData(values);
        return baseResp;
    }

    @Override
    public BaseResp search(String sv, Address address) {
        BaseResp baseResp = new BaseResp();
        List<Business> all = businessMapper.search(sv);
        List<Business> blist3 = ListCaculate.distanceB(all,address);
        baseResp.setData(blist3);
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp login(Business business) {
        BaseResp baseResp = new BaseResp();
        if (StringUtils.isEmpty(business.getPhone())) {
            baseResp.setCode(2001);
            baseResp.setMessage("手机号不能为空");
            return baseResp;
        }
        if (StringUtils.isEmpty(business.getPassword())) {
            baseResp.setCode(2002);
            baseResp.setMessage("密码不能为空");
        }
        if (!StringUtils.isEmpty(business.getPassword())) {
            Business byPhone = businessRepository.findByPhone(business.getPhone());
            if(byPhone==null){
                baseResp.setCode(200);
                baseResp.setMessage("用户不存在");
                return baseResp;
            }
            if(byPhone.getPassword().equals(business.getPassword())) {
                UUID uuid = UUID.randomUUID();
                JwtUtils jwtUtils = new JwtUtils();
                Map map = new HashMap<>();
                map.put("id", byPhone.getId());
                map.put("userName", byPhone.getBname());
                String token = jwtUtils.securtToken(map);
                redisTemplate.opsForValue().set(uuid.toString(),token);
                baseResp.setCode(200);
                baseResp.setMessage("登陆成功");
                baseResp.setData(uuid);
                return baseResp;
            } else {
                baseResp.setCode(203);
                baseResp.setMessage("密码错误");
                return baseResp;
            }
        }
        return null;
    }

    @Override
    public BaseResp findByPhone(String phone) {
        BaseResp baseResp = new BaseResp();
        Business byPhone = businessRepository.findByPhone(phone);
        if(byPhone!=null){
            baseResp.setData(byPhone);
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(2001);
        return baseResp;
    }

    @Override
    public BaseResp update(Business business) {
        BaseResp baseResp = new BaseResp();
        Business business1 = businessRepository.saveAndFlush(business);
        if(business1!=null){
            baseResp.setCode(200);
            baseResp.setMessage("修改成功");
            return baseResp;
        }
        baseResp.setCode(2001);
        baseResp.setMessage("修改失败");
        return baseResp;
    }

    @Override
    public BaseResp caculate(Address address) {
        BaseResp baseResp = new BaseResp();
            List<Business> all = businessRepository.findAll();
            List<Business> blist3 = ListCaculate.distanceB(all,address);
            baseResp.setData(blist3);
            baseResp.setCode(200);
            return baseResp;
    }
    @Override
    public BaseResp bsort(Address address) {
        BaseResp baseResp = new BaseResp();
        List<Business> all = businessRepository.findAll();
        List<Business> blist1 = ListCaculate.distanceB(all,address);
        List<Business> blist2 = ListSort.busDisJ(blist1);
        baseResp.setData(blist2);
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp btsort(Integer tid, Address address) {
        BaseResp baseResp = new BaseResp();
        List<BusinessAndType> byType = businessMapper.findByType(tid);
        if(byType!=null){
            List<BusinessAndType> businessAndTypes = ListCaculate.distanceBT(byType, address);
            List<BusinessAndType> businessAndTypes1 = ListSort.btDisJ(businessAndTypes);
            baseResp.setData(businessAndTypes1);
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(2001);
        return baseResp;
    }
}
